package MyGui;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
/**
 * Title:        iPAD
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:      ABB Inc.
 * @author Fangxing Li
 * @version 1.0
 */

public class OptSwtDlg extends MyDlgFrame {
    private Font  f = GuiConst.fontDefault;
    private int   GAP = 2;

    private static final int OBJ_EQP_COST   = 0;
    private static final int OBJ_CUST_COST  = 1;
    private static final int OBJ_MAIFI      = 2;
    private static final int OBJ_SAIFI      = 3;
    private static final int OBJ_SAIDI      = 4;
    private static final int CNSTRNT_EQP_COST   = 5;
    private static final int CNSTRNT_CUST_COST  = 6;
    private static final int CNSTRNT_MAIFI      = 7;
    private static final int CNSTRNT_SAIFI      = 8;
    private static final int CNSTRNT_SAIDI      = 9;
    private static final int POPULAION      = 10;
    private static final int MUTATION       = 11;
    private static final int COST_MANU_SWT  = 12;
    private static final int COST_LINE_REC  = 13;
    private static final int COST_AUTOMATE  = 14;
    //total number of text fields
    private static final int NUM_TEXTFIELD  = 15;

    private static final String[] asMainLabel = {
        "Equip. Cost",
        "Cust. Cost",
        "MAIFI",
        "SAIFI",
        "SAIDI",
        "Equip. Cost",
        "Cust. Cost",
        "MAIFI",
        "SAIFI",
        "SAIDI",
        "Population",
        "% Mutation",
        "Manual Switch",
        "Line Recloser",
        "Cost to Automate"
    };

    private static final int EXISTING   = 0;
    private static final int OPTIMIZED  = 1;

    private ZoneComboBox comboChoice;
    private JLabel[] alblMain;
    private JTextField[] atfMain;
    private JLabel[][] alblResults;
    private JButton   btnOptimize, btnVisualize, btnPrintResults;

    public OptSwtDlg(MainFrame parent, String title) {
        super(parent, title);

        alblMain= new JLabel[NUM_TEXTFIELD];
        atfMain = new JTextField[NUM_TEXTFIELD];
        for(int i=0;i<NUM_TEXTFIELD; i++){
            alblMain[i] = new JLabel( asMainLabel[i] );
            alblMain[i].setFont(f);
            atfMain[i] = new JTextField("0", 6);
            atfMain[i].setFont( f );
        }
        atfMain[OBJ_SAIDI].setText("1");
        atfMain[CNSTRNT_EQP_COST].setText("50000");
        atfMain[POPULAION].setText("100");
        atfMain[MUTATION].setText("0.5");
        atfMain[COST_MANU_SWT].setText("1000");
        atfMain[COST_LINE_REC].setText("15000");
        atfMain[COST_AUTOMATE].setText("5000");

        JPanel pObjective = createObjectivePanel();
        JPanel pConstraints = createConstraintsPanel();
        JPanel pEvlPara = createEvlParaPanel();
        JPanel pEquipCost = createEquipCostPanel();
        JPanel pResults = createResultsPanel();
        JPanel pButton = new JPanel();
          btnOptimize = new JButton("Optimize");
          pButton.add( btnOptimize);
          btnVisualize = new JButton("Visualize Results");
          pButton.add( btnVisualize);
          btnPrintResults = new JButton("Print Results");
          pButton.add( btnPrintResults);

        Container pane = this.getContentPane();
        pane.setLayout(gridBagLayout);

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(GAP, GAP, GAP, GAP);
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.WEST;
        c.gridheight = 1; c.gridwidth = 1;

        comboChoice = new ZoneComboBox();
        c.gridx=0; c.gridy=0; pane.add( comboChoice, c);
        c.gridx=1; c.gridy=0; pane.add( new JLabel("Area of Consideration"), c);

        c.weightx = 1; c.weighty = 1;
        c.gridx=0; c.gridy=1; pane.add(pObjective,c);
        c.gridx=0; c.gridy=2; pane.add(pConstraints,c);
        c.gridx=0; c.gridy=3; pane.add(pEvlPara,c);
        c.gridx=1; c.gridy=1; pane.add(pEquipCost,c);
        c.gridx=1; c.gridy=2; pane.add(pResults,c);
        c.gridx=1; c.gridy=3;
        c.gridx=0; c.gridy=4; c.gridwidth = 2; pane.add( pButton, c);

        this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        this.setSize(370,450);
        this.setLocation(100,100);
        this.setResizable(true);
    }

    private JPanel createObjectivePanel(){
        JPanel p = new JPanel( new GridLayout(5,2,1,1));
        p.setBorder( new TitledBorder("Objective Function"));

        for(int i= OBJ_EQP_COST; i<= OBJ_SAIDI; i++){
            p.add( atfMain[i]);
            p.add( alblMain[i]);
        }
        return p;
    }

    private JPanel createConstraintsPanel(){
        JPanel p = new JPanel( new GridLayout(5,2,1,1));
        p.setBorder( new TitledBorder("Constraints"));
        for(int i= CNSTRNT_EQP_COST; i<= CNSTRNT_SAIDI; i++){
            p.add( atfMain[i]);
            p.add( alblMain[i]);
        }
        return p;
    }

    private JPanel createEvlParaPanel(){
        JPanel p = new JPanel( new GridLayout(2,2,1,1));
        p.setBorder( new TitledBorder("Evolution Parameters"));

        for(int i= POPULAION; i<= MUTATION; i++){
            p.add( atfMain[i]);
            p.add( alblMain[i]);
        }
        return p;
    }

    private JPanel createEquipCostPanel(){
        JPanel p = new JPanel( new BorderLayout());
        p.setBorder( new TitledBorder("Equipment Cost($)"));

        JPanel p2 = new JPanel( new GridLayout(3,2,1,1));
        for(int i= COST_MANU_SWT; i<= COST_AUTOMATE; i++){
            p2.add( atfMain[i]);
            p2.add( alblMain[i]);
        }
        JPanel p3 = new JPanel( new GridLayout(3,1));
        p3.add( new JLabel("             "));
        p3.add( new JLabel("[A zero cost implies that the") );
        p3.add( new JLabel("equipement is not available]") );

        p.add( p2, BorderLayout.CENTER);
        p.add( p3, BorderLayout.SOUTH);
        return p;
    }

    private JPanel createResultsPanel(){
        JPanel p = new JPanel( new GridLayout(6,3));
        p.setBorder( new TitledBorder("Numerical Results($)"));

        alblResults = new JLabel[2][];
        alblResults[EXISTING] = new JLabel[5];
        alblResults[OPTIMIZED] = new JLabel[5];
        for(int i= 0; i< 6; i++){
            if( i==0 ){
                p.add( new JLabel("  "));
                p.add( new JLabel("Existing"));
                p.add( new JLabel("Optimized"));
            } else {
                p.add( new JLabel( this.asMainLabel[i-1] ) );
                alblResults[EXISTING][i-1] = new JLabel("--");
                alblResults[OPTIMIZED][i-1] = new JLabel("--");
                p.add( alblResults[EXISTING][i-1]);
                p.add( alblResults[OPTIMIZED][i-1]);
            }
        }
        return p;
    }
}